package com.yjy.fuyao.order.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yjy.fuyao.order.entity.CourseOrder;
import com.yjy.fuyao.order.service.CourseOrderService;
import com.yjy.fuyao.utils.result.R;
import com.yjy.fuyao.utils.result.ResponseEnum;
import com.yjy.fuyao.utils.util.JwtUtils;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author yjy
 * @since 2022-05-14
 */
@RestController
@RequestMapping("/service/order")
public class CourseOrderController {
    @Resource
    private CourseOrderService orderService;

    @PostMapping("/create/{courseId}")
    public R save(@PathVariable String courseId, HttpServletRequest request){
        String orderId = orderService.saveOrder(courseId, JwtUtils.getUserIdByJwtToken(request));
        return R.ok().data("orderId",orderId);
    }

    @GetMapping("/getOrder/{orderId}")
    public R get(@PathVariable String orderId) {
        QueryWrapper<CourseOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("order_no",orderId);
        CourseOrder order = orderService.getOne(wrapper);
        return R.ok().data("item", order);
    }


    @GetMapping("/getOrders/{page}/{limit}")
    public R getOrdersByUid(@ApiParam(name = "page", value = "当前页码", required = true)
                            @PathVariable Long page,

                            @ApiParam(name = "limit", value = "每页记录数", required = true)
                            @PathVariable Long limit,

                            HttpServletRequest request) {
        Page<CourseOrder> orderPage = new Page<>(page,limit);

        String userId = JwtUtils.getUserIdByJwtToken(request);
        if(userId == ""){
            return R.setResult(ResponseEnum.LOGIN_AUTH_ERROR);
        }
        Map<String, Object> map = orderService.orderPageList(orderPage, userId);
        return R.ok().data(map);
    }

    /**
     * 生成二维码
     *
     * @return
     */
    @GetMapping("/createNative/{orderNo}")
    public R createNative(@PathVariable String orderNo) {
        Map map = orderService.createNative(orderNo);
        return R.ok().data(map);
    }

    @GetMapping("/queryPayStatus/{orderNo}")
    public R queryPayStatus(@PathVariable String orderNo) {
        //调用查询接口
        Map<String, String> map = orderService.queryPayStatus(orderNo);
        if (map == null) {//出错
            LambdaQueryWrapper<CourseOrder> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(CourseOrder::getOrderNo,orderNo);
            orderService.remove(queryWrapper);
            return R.error().message("支付出错或取消支付");
        }
        if (map.get("trade_state").equals("SUCCESS")) {//如果成功
            //更改订单状态
            orderService.updateOrderStatus(map);
            return R.ok().message("支付成功");
        }

        return R.ok().code(25000).message("支付中");
    }

    @GetMapping("isBuyCourse/{userid}/{id}")
    public boolean isBuyCourse(@PathVariable String userid,
                               @PathVariable String id) {
        //订单状态是1表示支付成功
        int count = orderService.count(new QueryWrapper<CourseOrder>().eq("user_id", userid).eq("course_id", id).eq("status", 1));
        if(count>0) {
            return true;
        } else {
            return false;
        }
    }
}

